using Org.Apache.Rocketmq;

namespace Vin.Extension.RocketMQ.Models;

public class VinProducer
{
    protected virtual ClientConfig? ClientConfig { get; set; }

    public Producer? Producer { get; set; }

    internal async Task Init(ClientConfig clientConfig, List<string> topics, int maxAttempts = 3,
        ITransactionChecker? checker = null)
    {
        this.ClientConfig ??= clientConfig;
        var builder = new Producer.Builder()
            .SetTopics(topics.ToArray())
            .SetClientConfig(this.ClientConfig);
        if (maxAttempts > 0)
        {
            builder.SetMaxAttempts(maxAttempts);
        }

        if (checker != null)
        {
            builder.SetTransactionChecker(checker);
        }

        this.Producer = await builder.Build();
    }

    public async Task<ISendReceipt?> Send(Message message)
    {
        if (this.Producer == null) return null;
        return await this.Producer.Send(message);
    }
}